package com.alexgaoyh.weixin.core;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.alexgaoyh.weixin.info.util.CarLineLuceneUtil;
import com.alexgaoyh.weixin.message.resp.Article;
import com.alexgaoyh.weixin.message.resp.NewsMessage;
import com.alexgaoyh.weixin.message.resp.TextMessage;
import com.alexgaoyh.weixin.util.Constant;
import com.alexgaoyh.weixin.util.MessageUtil;
import com.alexgaoyh.weixin.util.http.HttpClientUtilss;

public class CoreService {
	
	/** 
     * 处理微信发来的请求 
     *  
     * @param request 
     * @return 
     */  
    public static String processRequest(HttpServletRequest request) {
    	 String respMessage = null;  
         try {  
             // xml请求解析  
             Map<String, String> requestMap = MessageUtil.parseXml(request);  
   
             // 发送方帐号（open_id）  
             String fromUserName = requestMap.get("FromUserName");  
             // 公众帐号  
             String toUserName = requestMap.get("ToUserName");  
             // 消息类型  
             String msgType = requestMap.get("MsgType");  
   
             // 文本消息  
             if (msgType.equals(Constant.REQ_MESSAGE_TYPE_TEXT)) {
            	 
            	 
                 // 接收用户发送的文本消息内容  
                 String content = requestMap.get("Content"); 
                 
                 //websocket发送消息
                 System.out.println(request.getContextPath());
                 HttpClientUtilss.get("http://weixin.alexgaoyh.top/websocket/send/" + content);
                 
                 //带 '-'，意味着是要匹配起始地，目的地的车辆信息；
                 if(content.contains("-")) {
                	 
                	 TextMessage textMessage = new TextMessage();  
                	 textMessage.setToUserName(fromUserName);  
                	 textMessage.setFromUserName(toUserName);  
                	 textMessage.setCreateTime(new Date().getTime());  
                	 textMessage.setMsgType(Constant.RESP_MESSAGE_TYPE_TEXT);  
                	 textMessage.setFuncFlag(0);
                	 String[] _locations = content.split("-");
                	 String resultStr = CarLineLuceneUtil.carLineInfoWithStartEndNames(_locations[0], _locations[1]);
                	 textMessage.setContent(resultStr); 
                	 
                	 // 将文本消息对象转换成xml字符串  
                	 respMessage = MessageUtil.textMessageToXml(textMessage);  
                 } else if(content.equals("报表"))	{
                	 
                	 NewsMessage newsMessage = new NewsMessage();
                	 newsMessage.setToUserName(fromUserName);
                	 newsMessage.setFromUserName(toUserName);
                	 newsMessage.setCreateTime(new Date().getTime());
                	 newsMessage.setMsgType(Constant.RESP_MESSAGE_TYPE_NEWS);
                	 newsMessage.setFuncFlag(0);
                	 
                	 Article article1 = new Article();
                	 article1.setPicUrl("http://pica.nipic.com/2008-03-19/2008319183523380_2.jpg");
                	 article1.setTitle("article1");
                	 article1.setUrl("http://www.lenglianmajia.com");
                	 article1.setDescription("article1");
                	 
                	 Article article2 = new Article();
                	 article2.setPicUrl("http://pic28.nipic.com/20130402/9252150_190139450381_2.jpg");
                	 article2.setTitle("article2");
                	 article2.setUrl("http://www.baidu.com");
                	 article2.setDescription("article2");
                	 
                	 List<Article> list = new ArrayList<Article>();
                	 list.add(article1);
                	 list.add(article2);
                	 
                	 newsMessage.setArticles(list);
                	 newsMessage.setArticleCount(list.size());
                	 
                	 respMessage = MessageUtil.newsMessageToXml(newsMessage);
                	 
                 } else {
                	 TextMessage textMessage = new TextMessage();  
                	 textMessage.setToUserName(fromUserName);  
                	 textMessage.setFromUserName(toUserName);  
                	 textMessage.setCreateTime(new Date().getTime());  
                	 textMessage.setMsgType(Constant.RESP_MESSAGE_TYPE_TEXT);  
                	 textMessage.setFuncFlag(0);
                	 textMessage.setContent(content); 
                	 // 将文本消息对象转换成xml字符串  
                	 respMessage = MessageUtil.textMessageToXml(textMessage);  
                 }
                 
             }   
               
         } catch (Exception e) {  
             e.printStackTrace();  
         }  
         return respMessage;  
    }  
}
